package com.chaodriod.flyviewpager.indicator

import android.annotation.TargetApi
import android.content.Context
import android.os.Build
import android.support.annotation.LayoutRes
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.RelativeLayout

/**
 * Created by CHEN on 2018-5-6.
 */
abstract class IndicatorViewPager : RelativeLayout {

    constructor(context: Context?) : super(context)

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    constructor(context: Context?, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context?, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)

    private val mRootView: View by lazy {
        LayoutInflater.from(context).inflate(getLayout(), this, false)
    }
    protected val mIndicator: IndicatorView by lazy {
        getIndicatorView()
    }

    init {
        addView(mRootView)
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        mIndicator.setCurrentItem(mIndicator.mCurrentPosition)
    }

    protected val mViewPager: ViewPager by lazy {
        getViewPager()
    }
    protected var mPageAdapter: PagerAdapter? = null


    @LayoutRes
    protected abstract fun getLayout(): Int

    protected abstract fun getViewPager(): ViewPager

    protected abstract fun getIndicatorView(): IndicatorView

    open fun setPageAdapter(adapter: PagerAdapter): IndicatorViewPager {
        mPageAdapter = adapter
        mViewPager.adapter = adapter
        return this
    }

    open fun addOnPageChangeListener(listener: ViewPager.OnPageChangeListener): IndicatorViewPager {
        mViewPager.addOnPageChangeListener(listener)
        return this
    }

    open fun getIndicatorItemView(position: Int): View {
        return mIndicator.getItem(position)
    }

}